<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png"/>
        <span class="title">{{ systemName }}</span>
      </div>
      <div class="desc">Ant Design 是西湖区最具影响力的 Web 设计规范</div>
    </div>
    <div class="login">
      <a-form @submit="onSubmit" :form="form">
        <a-tabs v-model="activeKey" default-active-key="1" size="large" :tabBarStyle="{textAlign: 'center'}"
                style="padding: 0 2px;">
          <a-tab-pane tab="专家登录" key="1">
            <a-alert type="error" :closable="true" v-show="error" :message="error" showIcon
                     style="margin-bottom: 24px;"/>
            <!--账号-->
            <a-form-item>
              <a-input
                  autocomplete="autocomplete"
                  size="large"
                  placeholder="admin"
                  v-decorator="['name', {rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
              >
                <a-icon slot="prefix" type="user"/>
              </a-input>
            </a-form-item>
            <!--密码-->
            <a-form-item>
              <a-input
                  size="large"
                  placeholder="888888"
                  autocomplete="autocomplete"
                  type="password"
                  v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
              >
                <a-icon slot="prefix" type="lock"/>
              </a-input>
            </a-form-item>
          </a-tab-pane>

          <a-tab-pane tab="会员单位负责人" key="2">
            <a-alert type="error" :closable="true" v-show="error" :message="error" showIcon
                     style="margin-bottom: 24px;"/>
            <!--账号-->
            <a-form-item>
              <a-input
                  autocomplete="autocomplete"
                  size="large"
                  placeholder="admin"
                  v-decorator="['name', {rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
              >
                <a-icon slot="prefix" type="user"/>
              </a-input>
            </a-form-item>
            <!--密码-->
            <a-form-item>
              <a-input
                  size="large"
                  placeholder="888888"
                  autocomplete="autocomplete"
                  type="password"
                  v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
              >
                <a-icon slot="prefix" type="lock"/>
              </a-input>
            </a-form-item>
          </a-tab-pane>

          <a-tab-pane tab="管理员" key="3">
            <a-alert type="error" :closable="true" v-show="error" :message="error" showIcon
                     style="margin-bottom: 24px;"/>
            <!--账号-->
            <a-form-item>
              <a-input
                  autocomplete="autocomplete"
                  size="large"
                  placeholder="admin"
                  v-decorator="['name', {rules: [{ required: true, message: '请输入账户名', whitespace: true}]}]"
              >
                <a-icon slot="prefix" type="user"/>
              </a-input>
            </a-form-item>
            <!--密码-->
            <a-form-item>
              <a-input
                  size="large"
                  placeholder="888888"
                  autocomplete="autocomplete"
                  type="password"
                  v-decorator="['password', {rules: [{ required: true, message: '请输入密码', whitespace: true}]}]"
              >
                <a-icon slot="prefix" type="lock"/>
              </a-input>
            </a-form-item>
          </a-tab-pane>
        </a-tabs>
        <div>
          <a-checkbox :checked="true">自动登录</a-checkbox>
          <a style="float: right">忘记密码</a>
        </div>
        <a-form-item>
          <!--          htmlType="submit"-->
          <a-button :loading="logging" @click="loginin" style="width: 100%;margin-top: 24px" size="large"
                    htmlType="submit"
                    type="primary">登录
          </a-button>
        </a-form-item>
        <div>
          其他登录方式
          <a-icon class="icon" type="alipay-circle"/>
          <a-icon class="icon" type="taobao-circle"/>
          <a-icon class="icon" type="weibo-circle"/>
          <router-link style="float: right" to="/dashboard/workplace">注册账户</router-link>
        </div>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '@/layouts/CommonLayout'
import {login, getRoutesConfig} from '@/services/user'
import {setAuthorization} from '@/utils/request'
import {loadRoutes} from '@/utils/routerUtil'
import {mapMutations} from 'vuex'
import Axios from "axios"

export default {
  name: 'Login',
  components: {CommonLayout},
  data() {
    return {
      url: this.$store.state.apiUrl + ':' + this.$store.state.apiPort,
      logging: false,
      activeKey: '1',
      error: '',
      form: this.$form.createForm(this),
      specialist: {},
      declartant: {},
      admin: {},
    }
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName
    },
  },

  methods: {
    ...mapMutations('account', ['setUser', 'setPermissions', 'setRoles']),
    onSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err) => {
        if (!err) {
          this.logging = true
          const name = this.form.getFieldValue('name')
          const password = this.form.getFieldValue('password')
          // 登录
          this.realLogin(name, password)
          // login(name, password).then(this.afterLogin)
        }
      })
    },
    afterLogin(res) {
      this.logging = false
      const loginRes = res.data
      console.log(loginRes.data)
      if (loginRes.code >= 0) {
        const {user, permissions, roles} = loginRes.data
        this.setUser(user)
        this.setPermissions(permissions)
        this.setRoles(roles)
        setAuthorization({token: loginRes.data.token, expireAt: new Date(loginRes.data.expireAt)})
        this.$message.success('登陆成功')
        // 设置权限
        switch (this.activeKey) {
          case '1':
            loginRes.data.roles[0].id = 'specialist'
            loginRes.data.roles[0].operation.push('specialist')
            this.$store.state.specialist = this.specialist
            break
          case '2':
            loginRes.data.roles[0].id = 'declarant'
            loginRes.data.roles[0].operation.push('declarant')
            this.$store.state.declartant = this.declartant
            break
          default:
            loginRes.data.roles[0].id = 'admin'
            loginRes.data.roles[0].operation.push('admin')
        }
       // console.log(loginRes.data)
        // 获取路由配置
        getRoutesConfig().then(result => {
          const routesConfig = result.data.data
          loadRoutes(routesConfig)
          // 页面跳转传参
          if (this.activeKey === '1') {
            this.$router.push({
              name: '专家首页',
            })
          } else if (this.activeKey === '2') {
            this.$router.push({
              name: '申报人首页',
            })
          } else {
            this.$router.push({name: '管理员首页'})
          }
          this.$message.success(loginRes.message, 3)
        })
      } else {
        this.error = loginRes.message
      }
    },
    loginin() {
      // console.log(this.activeKey);
      //this.$router.push('/dashboard/Speciallistworkplace')
      // if (this.activeKey=='1'){
      //     this.$router.push('/dashboard/Speciallistworkplace')
      // }else {
      //     this.$router.push('/dashboard/workplace')
      // }
    },
    // 真实连接后端登录函数
    realLogin(name, password) {
      switch (this.activeKey) {
        case '1':
          this.specialistLogin(name, password)
          break
        case '2':
          this.declarantLogin(name, password)
          break
        default:
          this.managementLogin(name, password)
      }
    },
    // 专家登录
    specialistLogin(name, password) {
      console.log(name, password)
      Axios.post(this.url + '/pxManagerSystem/validateSpecialLogin', {
        specialist_name: name,
        specialist_pass: password
      }).then(res => {
      //  console.log(res.data.data)
        if (res.data.state === 200) {
          this.specialist = res.data.data.currentSpeciallistInfo
          // 登陆成功，借用一下示例的接口
          login('admin', '888888').then(this.afterLogin)
        } else {
          // 登陆失败
          login('123', '123').then(this.afterLogin)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 企业负责人登录
    declarantLogin(name, password) {
      Axios.post(this.url + '/pxManagerSystem/validateCompanyUsersLogin', {
        company_username: name,
        company_userpass: password
      }).then(res => {
        if (res.data.state === 200) {
          this.declartant = res.data.data.currentCompanyUser
          // 登陆成功，借用一下示例的接口
          login('admin', '888888').then(this.afterLogin)
        } else {
          // 登陆失败
          login('123', '123').then(this.afterLogin)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 管理员登录
    managementLogin(name, password) {
      Axios.post(this.url + '/pxManagerSystem/validateAdminLogin', {
        admin_username: name,
        admin_pass: password
      }).then(res => {
        if (res.data.state === 200) {
          // 登陆成功，借用一下示例的接口
          login('admin', '888888').then(this.afterLogin)
        } else {
          // 登陆失败
          login('123', '123').then(this.afterLogin)
        }
      }).catch(err => {
        console.log(err)
      })
    },
    changeKey() {
      this.keys = 1;
      console.log(this.keys);
    }
  }
}
</script>

<style lang="less" scoped>
.common-layout {
  .top {
    text-align: center;

    .header {
      height: 44px;
      line-height: 44px;

      a {
        text-decoration: none;
      }

      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }

      .title {
        font-size: 33px;
        color: @title-color;
        font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica, sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }

    .desc {
      font-size: 14px;
      color: @text-color-second;
      margin-top: 12px;
      margin-bottom: 40px;
    }
  }

  .login {
    width: 368px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }

    .icon {
      font-size: 24px;
      color: @text-color-second;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: @primary-color;
      }
    }
  }
}
</style>
